/**
 * 2016年9月27日
 */
package algorithm.nowcoder.string;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author Alex
 *
 */
class MyClassLoader extends ClassLoader{  
    public Class<?> loadClass(String name) throws ClassNotFoundException{  
        try {    
            String fileName=name.substring(name.lastIndexOf(".")+1)+".class";  
            InputStream is=getClass().getResourceAsStream(fileName);    
            if(is==null){   return super.loadClass(name); 
            }else{  
                byte[] b=new byte[is.available()];  
                is.read(b);  
                return defineClass(name,b,0,b.length);      
            }  
        } catch (IOException e) {  throw new ClassNotFoundException();   }  
    }  
}  

public class LoaderTest {
	public static void main(String args[]) throws InstantiationException, IllegalAccessException, ClassNotFoundException{
		MyClassLoader loader = new MyClassLoader();
		algorithm.nowcoder.string.A a = new algorithm.nowcoder.string.A();
		Object c = loader.loadClass("algorithm.nowcoder.string.A").newInstance();
		System.out.println(a.getClass().getCanonicalName());
		System.out.println(c.getClass().getCanonicalName());
		System.out.println();
		a=(algorithm.nowcoder.string.A)c;//exception?
		
	}
}
